package tests;

import common.Utils;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.Duration;

public class LoginPage extends Utils {
    public static String url = "http://127.0.0.1:8888/login.html";

    public LoginPage() {
        super(url);
    }

//    1.检查页面是否能正常加载成功
    public void loginPageRight() throws IOException {
        driver.findElement(By.cssSelector("body > div.nav"));
        driver.findElement(By.cssSelector("body > div.login-container > div > div:nth-child(2) > span"));
        driver.findElement(By.cssSelector("#password"));
//        getScreenShot("test01-loginPageRight");
//        driver.quit();
    }

//    2.登录功能 -- 正常登录
    public void loginSucc() throws IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("测试用户3");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#inputCaptcha")).sendKeys("26588");
//        点击登录按钮
        driver.findElement(By.cssSelector("#submit")).click();
//        处理弹窗
//        隐式等待不起作用，警示框需要加显示等待
        new WebDriverWait(driver, Duration.ofSeconds(3)).until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
//        检查点击登录后是否登录成功，可以通过聊天页面特有的元素进行判断
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-session"));
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.tab > div.tab-friend"));
//        可以页面标题来检查是否正确成功登录
        String expect = driver.getTitle();
//        使用断言操作
        assert expect.equals("网页聊天程序");
        driver.navigate().back();
    }

//    2.登录功能 -- 异常登录 -- 用户名为空
    public void loginFail1() throws IOException, InterruptedException {
//        可以通过刷新保证输入框没有文本信息
        driver.navigate().refresh();
//        或者是clear方法
        driver.findElement(By.cssSelector("#username")).sendKeys("");
//        错误的密码
        driver.findElement(By.cssSelector("#password")).sendKeys("1234");
        driver.findElement(By.cssSelector("#inputCaptcha")).sendKeys("26588");
        driver.findElement(By.cssSelector("#submit")).click();

        new WebDriverWait(driver, Duration.ofSeconds(3)).until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
//        获取弹窗的信息
        String text = alert.getText();
        assert text.equalsIgnoreCase("所输入的账号或者密码不能为空！");
        alert.accept();
    }

    //    4.登录功能 -- 异常登录 -- 用户名为错误：zhang 密码正确：123 验证码正确
    public void loginFail2() throws IOException, InterruptedException {
//        可以通过刷新保证输入框没有文本信息
        driver.navigate().refresh();
//        Thread.sleep(60);
        driver.findElement(By.cssSelector("#username")).sendKeys("zhang");
//        错误的密码
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#inputCaptcha")).sendKeys("26588");
        driver.findElement(By.cssSelector("#submit")).click();

        new WebDriverWait(driver, Duration.ofSeconds(3)).until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
//        获取弹窗的信息
        String text = alert.getText();
        assert text.equalsIgnoreCase("账号或者密码错误！");
        alert.accept();
//        driver.navigate().back();
//        driver.quit();
    }



//    5.登录功能，检查“注册”按钮是否正常 -- 失败 -->  地址栏只显示data，所以找不到对应的元素  --> 已经解决
    public void chectRegisterButton() throws IOException, InterruptedException {
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("body > div.login-container > div > div.row-register > a")).click();
        getScreenShot("test03-LoginPage-chectRegisterButton");
        String title = driver.getTitle();
        assert title.equals("Register");
        driver.quit();
    }
}
